<?php

namespace app\api\controller\v1;

use think\Controller;
use app\service\User;
use Lcobucci\JWT\Parser;
use app\common\model\ConfigModel;
use app\common\model\OrderModel;
use app\common\model\UserModel;

Class Financial extends Controller
{
    // 充值
    public function recharge()
    {
        $money = input('param.money');
        $token = input('param.token');

        $parse = (new Parser())->parse($token);
        $data  = json_decode($parse->getClaim('data'), true);

        if ($money == 1) {
            $money = 10;
        } elseif ($money == 2) {
            $money = 20;
        } elseif ($money == 3) {
            $money = 50;
        } elseif ($money == 4) {
            $money = 100;
        } elseif ($money == 5) {
            $money = 200;
        } elseif ($money == 6) {
            $money = 500;
        } elseif ($money == 7) {
            $money = 1000;
        } elseif ($money == 8) {
            $money = 2000;
        }

        $this->assign([
          'money' => $money,
          'loginUser' => (new User)->getLoginUser($data['id'])
        ]);
        $c_model = new ConfigModel();
        $this->assign([
            'wxcode' => $c_model->get_value('wxcode'),
            'wxzh' => $c_model->get_value('wxzh'),
            'zfbcode' => $c_model->get_value('zfbcode'),
            'zfbzh' => $c_model->get_value('zfbzh'),
            'bank_name' => $c_model->get_value('bank_name'),
            'bank_account' => $c_model->get_value('bank_account'),
            'bank_people' => $c_model->get_value('bank_people'),
            'wxkf' => $c_model->get_value('wxkf'),
            'qqkf' => $c_model->get_value('qqkf'),
            'site_code' => $c_model->get_value('site_code'),
        ]);
        return $this->fetch('financial/recharge');
    }

    // 充值保存数据库
    public function rechargeApi()
    {
        $param = input('param.');
        $oModel = new OrderModel;
        $uModel = new UserModel;

        $uid = $uModel->getUidByUser($param['user']);

        $map['uid'] = $uid;
        $map['status'] = 0;
        if ($oModel->where($map)->count() > 0) {
            $this->error('您还有一个订单未审核，无法充值');
        }

        $uinfo = $uModel->getBaseInfoByUid($uid);
        $data = [
            'deal_type' => ORDER_TYPE_RECHARGE,
            'uid'   => $uid,
            'order_no' => 'RG'.date('TmdHis').mt_rand(10,99),
            'deal_before' => $uinfo['balance'],
            'deal_amount' => $param['money'],
            'deal_after' => $uinfo['balance'] + $param['money']
        ];
        if ($oModel->save($data) > 0) {
            $this->success('充值成功，请等待后台审核');
        }

        return $this->error('充值错误');
    }
}